/*
 * Copyright (c) 2009, 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/gfx/IGraphics3DDevice.h"
#include "alkes/gfx/LineShape3DImpl.h"
#include "alkes/gfx/RectangleShape3DImpl.h"
#include "alkes/gfx/ImageShape3DImpl.h"
#include "alkes/gfx/PolylineShape3DImpl.h"
#include "alkes/gfx/RenderTarget.h"

namespace alkes {

IGraphics3DDevice::IGraphics3DDevice()
: target_(0)
{
}

IGraphics3DDevice::~IGraphics3DDevice()
{
}

void IGraphics3DDevice::onChangeRenderTarget(RenderTarget* /*old_target*/, RenderTarget* /*new_target*/)
{
}

void IGraphics3DDevice::setRenderTarget(RenderTarget* target)
{
    RenderTarget* old_target = target_;

    if (old_target != target)
    {
        target_ = target;
        onChangeRenderTarget(old_target, target);
    }
}

RenderTarget* IGraphics3DDevice::getRenderTarget()
{
    return target_;
}

const Rect IGraphics3DDevice::getTargetRect() const
{
    if (target_)
        return target_->getTargetRect();

    return Rect::ZERO;
}

Program* IGraphics3DDevice::createShaderProgram(const char* vertex_shader_source, const char* fragment_shader_source)
{
    return createShaderProgram(
        createVertexShader(vertex_shader_source),
        createFragmentShader(fragment_shader_source));
}

LineShape* IGraphics3DDevice::createLineShape(uint32_t num)
{
    return LineShape3DImpl::create(this, num);
}

RectangleShape* IGraphics3DDevice::createRectangleShape(uint32_t num)
{
    return RectangleShape3DImpl::create(this, num);
}

ImageShape* IGraphics3DDevice::createImageShape(uint32_t num)
{
    return ImageShape3DImpl::create(this, num);
}

PolylineShape* IGraphics3DDevice::createPolylineShape(uint32_t num)
{
    return PolylineShape3DImpl::create(this, num);
}

void IGraphics3DDevice::translate(const Vector3D& offset)
{
    translate(offset.x, offset.y, offset.z);
}

void IGraphics3DDevice::setTextureFilter(TexFilterValue min_filter, TexFilterValue mag_filter)
{
    setTextureFilter(min_filter, mag_filter, 0);
}

void IGraphics3DDevice::getTextureFilter(TexFilterValue* min_filter, TexFilterValue* mag_filter)
{
    return getTextureFilter(min_filter, mag_filter, 0);
}

void IGraphics3DDevice::setTextureAddressMode(TexAddressMode u_mode, TexAddressMode v_mode)
{
    setTextureAddressMode(u_mode, v_mode, 0);
}

void IGraphics3DDevice::getTextureAddressMode(TexAddressMode* u_mode, TexAddressMode* v_mode)
{
    getTextureAddressMode(u_mode, v_mode, 0);
}

void IGraphics3DDevice::fill(const Color& color)
{
    clearBuffer(AL_BUFFER_COLOR|AL_BUFFER_DEPTH, color);
}

bool IGraphics3DDevice::drawShape(LineShape* line_shape, uint32_t start, uint32_t count)
{
    LineShape3DImpl* shape = restrict_cast<LineShape3DImpl*>(line_shape);

    if (!shape)
        return false;

    return shape->renderTo(this, start, count);
}

bool IGraphics3DDevice::drawShape(RectangleShape* rectangle_shape, uint32_t start, uint32_t count)
{
    RectangleShape3DImpl* shape = restrict_cast<RectangleShape3DImpl*>(rectangle_shape);

    if (!shape)
        return false;

    return shape->renderTo(this, start, count);
}

bool IGraphics3DDevice::drawShape(ImageShape* image_shape, uint32_t start, uint32_t count)
{
    ImageShape3DImpl* shape = restrict_cast<ImageShape3DImpl*>(image_shape);

    if (!shape)
        return false;

    return shape->renderTo(this, start, count);
}

bool IGraphics3DDevice::drawShape(PolylineShape* polyline_shape, uint32_t start, uint32_t count)
{
    PolylineShape3DImpl* shape = restrict_cast<PolylineShape3DImpl*>(polyline_shape);

    if (!shape)
        return false;

    return shape->renderTo(this, start, count);
}

}
